package com.ats.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.ats.dao.FindMsgDao_cl;
import com.ats.model.Notice;
import com.ats.util.JDBCUtil;

import net.sf.json.JSONArray;


public class FindMsgDaoImpl_cl implements FindMsgDao_cl{

	@Override
	public JSONArray FindMsgByIid(int iid) {
		JSONArray jso = new JSONArray();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ats_notice.nid,ats_notice.ndata,ats_notice.ntext ");
		sql.append("FROM ats_notice ");
		sql.append(" WHERE iid=?");
		Connection  con = JDBCUtil.getConnection();
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1,iid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Notice  no = new Notice();
				no.setNid(rs.getInt("nid"));
				no.setNtext(rs.getString("ntext"));
				no.setNdata(rs.getString("ndata"));
				jso.add(no);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return jso;
	}

	@Override
	public JSONArray FindMsgBySid(int sid) {
		JSONArray jso = new JSONArray();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ats_notice.nid,ats_notice.ndata,ats_notice.ntext ");
		sql.append("FROM ats_notice ");
		sql.append(" WHERE sid=?");
		Connection  con = JDBCUtil.getConnection();
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1,sid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Notice  no = new Notice();
				no.setNid(rs.getInt("nid"));
				no.setNtext(rs.getString("ntext"));
				no.setNdata(rs.getString("ndata"));
				jso.add(no);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return jso;
	}

	@Override
	public JSONArray FindMsgByTid(int tid) {
		JSONArray jso = new JSONArray();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ats_notice.nid,ats_notice.ndata,ats_notice.ntext ");
		sql.append("FROM ats_notice ");
		sql.append(" WHERE tid=?");
		Connection  con = JDBCUtil.getConnection();
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1,tid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Notice  no = new Notice();
				no.setNid(rs.getInt("nid"));
				no.setNtext(rs.getString("ntext"));
				no.setNdata(rs.getString("ndata"));
				jso.add(no);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return jso;
		
	}

}
